package cn.com.travelman.service.push.factory.getui;

import java.io.IOException;

import cn.com.travelman.service.push.bean.PushLinkMessage;
import cn.com.travelman.service.push.bean.PushNotificationMessage;
import cn.com.travelman.service.push.bean.PushTransmissionMessage;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;

public class PushMessageToSingle extends PushMessage {
	//
	public PushMessageToSingle(String master, String cid, String host) {
		super.setMaster(master);
		super.setCid(cid);
		super.setHost(host);
	}

	private SingleMessage initMessage() {
		SingleMessage message = new SingleMessage();
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 3600 * 1000);
		// message.setPushNetWorkType(1);
		// //判断是否客户端是否wifi环境下推送，1为在WIFI环境下，0为不限制网络环境。
		return message;
	}
	
	private Target initTarget(String appId) {
		Target target = new Target();
		target.setAppId(appId);
		target.setClientId(this.getCid());
		// 用户别名推送，cid和用户别名只能2者选其一
		// String alias = "个";
		// target.setAlias(alias);
		return target;
	}

	public boolean pushTransmission(
			PushTransmissionMessage content) {
		boolean result = false;
		//
		TransmissionTemplate template = this.initTemplate(content);
		//
		IGtPush push = new IGtPush(this.getHost(),
				content.getAppKey(), this.getMaster());
		try {
			push.connect();
		} catch (IOException e) {
			e.printStackTrace();
			return result;
		}

		SingleMessage message = initMessage();
		message.setData(template);
		//
		Target target = initTarget(content.getAppId());
		//
		IPushResult ret = push.pushMessageToSingle(message, target);
		if ("successed_online".equals(ret.getResponse().toString())
				|| "successed_offline".equals(ret.getResponse().toString())) {
			System.out.println(ret.getResponse().toString());
			result = true;
		}
		return result;
	}

	//
	public boolean pushLink(PushLinkMessage content) {
		boolean result = false;
		//
		LinkTemplate template = this.initTemplate(content);
		//
		IGtPush push = new IGtPush(this.getHost(),
				content.getAppKey(), this.getMaster());
		try {
			push.connect();
		} catch (IOException e) {
			e.printStackTrace();
			return result;
		}

		SingleMessage message = initMessage();
		message.setData(template);
		//
		Target target = initTarget(content.getAppId());
		//
		IPushResult ret = push.pushMessageToSingle(message, target);
		if ("successed_online".equals(ret.getResponse().toString())
				|| "successed_offline".equals(ret.getResponse().toString())) {
			System.out.println(ret.getResponse().toString());
			result = true;
		}
		return result;
	}

	//
	public boolean pushNotification(PushNotificationMessage content) {
		boolean result = false;
		//
		NotificationTemplate template = this.initTemplate(content);
		//
		IGtPush push = new IGtPush(this.getHost(),
				content.getAppKey(), this.getMaster());
		try {
			push.connect();
		} catch (IOException e) {
			e.printStackTrace();
			return result;
		}

		SingleMessage message = initMessage();
		message.setData(template);
		//
		Target target = initTarget(content.getAppId());
		//
		IPushResult ret = push.pushMessageToSingle(message, target);
		if ("successed_online".equals(ret.getResponse().toString())
				|| "successed_offline".equals(ret.getResponse().toString())) {
			System.out.println(ret.getResponse().toString());
			result = true;
		}
		return result;
	}
}
